package com.ryl.yomall.product.utils;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Date 20:19
 * @Author ryl
 */

public class FileUtil {
    public static String getUrl(String conf,MultipartFile file) throws IOException, MyException {
        String path = ClassUtils.getDefaultClassLoader().
                getResource( conf ).getPath();
        ClientGlobal.init(path);
        TrackerClient trackerClient = new TrackerClient();
        //连接Tracker
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageServer storageServer = null;
        //连接上了存储节点
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
        //文件的扩展名
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传图片
        String p = storageClient1.upload_file1(
                file.getBytes(),
                ext, null);
        //    group1/M00/00/00/wKjIgl_gG4SAKtDeAAT8_AoEzOU590.jpg
        System.out.println(p);
        return p;
    }
}
